package com.lc.tgxm.activity;

import android.os.Bundle;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.tgxm.R;
import com.lc.tgxm.adapter.HomeListAdapter;
import com.lc.tgxm.conn.SaleClueListAsyGet;
import com.lc.tgxm.model.HomeListmodel;
import com.lc.tgxm.model.HomeModelDTO;
import com.zcx.helper.adapter.AppRecyclerAdapter;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;

/**
 * MyListActivity
 * Created by Lei on 2018/1/18.
 */
public class MyListActivity extends BaseActivity {
    @BoundView(R.id.xrv_main)
    private XRecyclerView xRecyclerView;
    private HomeListAdapter homeListAdapter;// 主列表的Adapter

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenViewAndBack(R.layout.activity_mylist, R.string.mylist);

        // 主列表
        homeListAdapter = new HomeListAdapter(this) {
            // 主列表Item点击事件
            @Override
            public void onItemClick(int position, HomeListmodel homelistmodel) {
                UtilToast.show("position: " + position);
            }
        };

        // 初始化 XRecyclerView
        xRecyclerView.setLayoutManager(homeListAdapter.verticalLayoutManager(context));
        xRecyclerView.setAdapter(homeListAdapter);
        xRecyclerView.setPullRefreshEnabled(true);// 下拉刷新
        xRecyclerView.setLoadingMoreEnabled(true); // 上滑加载
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
//                // 下拉刷新
//                currentIndex = 1;
//
//                // 走接口
//                carlifeOrderListPostAsy.page = "1";
//                carlifeOrderListPostAsy.execute(context, 1);

                testData(1);
                xRecyclerView.loadMoreComplete();
                xRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                // 上滑加载
//                currentIndex++;
//                if (currentIndex > totalPage) {
//                    xRecyclerView.loadMoreComplete();
//                    xRecyclerView.refreshComplete();
//                    UtilToast.show(R.string.to_alloaded);
//                } else {
//                    // 走接口
//                    carlifeOrderListPostAsy.page = currentIndex + "";
//                    carlifeOrderListPostAsy.execute(context, 2);
//                }

                testData(2);
                xRecyclerView.loadMoreComplete();
                xRecyclerView.refreshComplete();
            }
        });

        // 初始化 走接口
//        carlifeOrderListPostAsy.page = "1";
//        carlifeOrderListPostAsy.execute(context, 1);

        testData(1);
    }

    private void testData(int type) {
        ArrayList<AppRecyclerAdapter.Item> arrayList = new ArrayList<>();
        arrayList.addAll(HomeListmodel.testData(3));

        if (type == 1) {
            homeListAdapter.setList(arrayList);
        } else {
            homeListAdapter.addList(arrayList);
        }

    }

    // 车生活 订单 主列表接口
    private SaleClueListAsyGet carlifeOrderListPostAsy = new SaleClueListAsyGet(new AsyCallBack<HomeModelDTO>() {
        @Override
        public void onSuccess(String toast, int type, HomeModelDTO homeDTO) throws Exception {
            if (!homeDTO.arrayList.isEmpty()) {
                totalPage = homeDTO.totalPage;
                if (type == 1) {// 下拉刷新
                    homeListAdapter.setList(homeDTO.arrayList);
                } else {// 上滑加载
                    homeListAdapter.addList(homeDTO.arrayList);
                }
            } else {// 没有数据
                UtilToast.show(R.string.to_empty);
            }
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            xRecyclerView.loadMoreComplete();
            xRecyclerView.refreshComplete();
        }
    });


}